(function() {
    'use strict';

    angular
        .module('byibserverApp')
        .controller('MildCaseDialogController', MildCaseDialogController);

    MildCaseDialogController.$inject = ['$timeout', '$scope', '$stateParams', '$uibModalInstance', 'entity', 'MildCase', 'Product'];

    function MildCaseDialogController ($timeout, $scope, $stateParams, $uibModalInstance, entity, MildCase, Product) {
        var vm = this;

        vm.mildCase = entity;
        vm.clear = clear;
        vm.save = save;
        getProduct();

        function getProduct() {
            Product.query({
                page: 0,
                size: 1000,
            }, function (data,headers) {
                vm.products = data;
            }, function (error) {

            });
        }

        $timeout(function (){
            angular.element('.form-group:eq(1)>input').focus();
        });

        function clear () {
            $uibModalInstance.dismiss('cancel');
        }

        function save () {
            vm.isSaving = true;
            if (vm.mildCase.id !== null) {
                MildCase.update(vm.mildCase, onSaveSuccess, onSaveError);
            } else {
                MildCase.save(vm.mildCase, onSaveSuccess, onSaveError);
            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('byibserverApp:mildCaseUpdate', result);
            $uibModalInstance.close(result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }


    }
})();
